package com.example.textview;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.example.textview.util.CustomWebView;

public class WebViewActivity extends AppCompatActivity {

    private CustomWebView mwebview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        mwebview=findViewById(R.id.wv);
       // mwebview.loadUrl("file:///android_asset/test.html");//加载本地Html

        //加载网络URL
        mwebview.getSettings().setJavaScriptEnabled(true);
        mwebview.setWebViewClient(new MyWebViewClient());//这样就不会加载外部的浏览器
        mwebview.setWebChromeClient(new MyWebChromeClient());//两个不同的方法
       // mwebview.loadUrl("https://m.bilibili.com/");//加载本地Html

       mwebview.loadUrl("file:///android_asset/test.html");//加载本地Html
    }



    class MyWebViewClient extends WebViewClient{

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.getUrl().toString());
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

        }


    }

    class MyWebChromeClient extends WebChromeClient{

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            setTitle(title);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK&&mwebview.canGoBack()){

            mwebview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }




}